const util = require("../../../utils/util")
Page({
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
this.getdata()
  },
  getdata(){
    let that = this;
    let _id = wx.getStorageSync('istokenid')
    wx.cloud.database().collection('chat_user').doc(
      _id
    ).get().then(res=>{
      console.log(res);
      that.setData({
        userInfoA:res.data
      })
    })
  },
//发表
  submit(){
    let that = this;
    let time = Date.now()
    let num = Math.random()
  //上传图片云端
    for(let i = 0; i< that.data.userImg.length; i++){
    console.log( String(that.data.userImg[i]));
      wx.cloud.uploadFile({
        cloudPath : `userList/${num}-${time}-xx.png`,
        filePath:String(that.data.userImg[i])
      }).then(res=>{
        console.log(res);
        console.log(res.fileID);
        wx.cloud.database().collection('userList').add({
          data:{
            nickName:that.data.userInfoA.nickName,
            faceImg:that.data.userInfoA.faceImg,
            text:that.data.textares,
            images:res.fileID,
            time:util.formatTime(new Date())
          }
        }).then(res=>{
          console.log(res);
        wx.navigateBack({
            success(res){
              wx.showToast({
                title: '发布成功',
              })
            }
        })
        })
      })
  }
  },

  //文本
  getValue(e){
    this.setData({
      textares:e.detail.value
    })
  },
  // 删除图片
  deleteimg(e){
this.data.userImg.splice(e.currentTarget.dataset.index,1)
this.setData({
  userImg:this.data.userImg
})
  },
  //上传本地图片
  chooseImage(){
    let that = this
    wx.chooseMedia({
      count: 9,
      mediaType: ['image','video'],
      sourceType: ['album', 'camera'],
      success(res) {
        let wuqi= []
        res.tempFiles.map(res=>
          wuqi.push(res.tempFilePath)
        )
        that.setData({
        userImg: wuqi
      })
        } 
    })
  }
})